<template>
  <div id="menu_id">
    <div class="menu_top">
      <p class="menu_note">
        注:<br />
        1.增加的新栏目为当前栏目下一行 <br />
        2.修改完的栏目一行需保存后提交审批才能生效
      </p>
      <div class="menu_add">
        <el-button size="mini" round @click="openPanel">增加</el-button>
      </div>
    </div>
    <el-table
      :data="menuList"
      style="width: 100%; margin-bottom: 20px; margin-top: 20px"
      row-key="id"
      border
      :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
      header-row-class-name="hhh"
    >
      <el-table-column prop="id" label="编号"> </el-table-column>
      <el-table-column prop="title" label="栏目名称"> </el-table-column>
      <el-table-column prop="icon" label="栏目图标" v-slot="$props">
        <template>
          <i :class="$props.row.icon"></i>
        </template>
      </el-table-column>
      <el-table-column prop="url" label="栏目地址"> </el-table-column>
      <el-table-column prop="status" label="状态" v-slot="$props">
        <template>
          <el-button
            v-if="$props.row.status === 0"
            size="mini"
            type="danger"
            plain
            round
            >已禁用</el-button
          >
          <el-button
            v-else-if="$props.row.status === 1"
            size="mini"
            type="success"
            plain
            round
            >已启用</el-button
          >
        </template>
      </el-table-column>
      <el-table-column label="操作" v-slot="$props">
        <template>
          <el-button size="mini" round @click="updateMenu($props.row)">
            修改
          </el-button>
        </template>
        <template>
          <el-button size="mini" round @click="delMenu($props.row.id)">
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <AddMenu
      ref="menu_ref"
      :isShow="isShow"
      :menuList="menuList"
      @reload_list="reload_list"
    ></AddMenu>
  </div>
</template>
<script>
import { mapActions, mapState } from "vuex";
import AddMenu from "../components/addMenu.vue";
export default {
  data() {
    return {
      isShow: { flag: false },
    };
  },
  created() {
    this.MenuListAction();
  },
  methods: {
    Del(id){
      //如果确定删除
      this.$http.post("/api/menudelete", { id }).then((res) => {
        if (res.code === 200) {
          this.MenuListAction();
        }
      });
    },
    delMenu(id) {
      this.$confirm("确定要删除菜单吗？", "删除菜单", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "error",
      })
        .then(() => {
          this.Del(id)
          this.$message({
            type: "success",
            message: "删除成功!",
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    updateMenu(obj) {
      // 打开模态框
      this.isShow.flag = true;
      // 赋值给模态框
      this.$refs.menu_ref.setFome(obj);//父组件当中通过虚拟dom调用子组件的方法，来传递参数
    }, //更新数据
    reload_list() {
      this.MenuListAction();
    }, //重载
    openPanel() {
      this.isShow.flag = true;
    },
    ...mapActions("MenuStore", ["MenuListAction"]),
  },
  computed: {
    ...mapState({ menuList: (state) => state.MenuStore.menuList }),
  },
  components: {
    AddMenu,
  },
};
</script>

<style>
#menu_id .menu_top {
  display: flex;
  justify-content: space-between;
}
#menu_id .menu_note {
  font-size: 12px;
  color: #c4c4c4;
  text-align: left;
}
#menu_id .menu_add > button {
  width: 80px;
  color: #ff5d3e;
  background-color: white;
}
</style>